<template>
  <div id="apptb">
    <div class="grouptitle">
      <div class="grouptitletext">单练人员报名信息</div>
    </div>

    <div class="contentall">
      <div class="contentallcontent">
        <div class="contentss">
          <!-- 每一行 -->

          <!--  -->
          <div class="inputall">
            <div class="inputleft">
              <label for="">项目编号</label>
              <input
                type="text"
                class="inputtextte"
                v-model="project.projectid"
              />
            </div>
            <div class="inputright">
              <label for="">项目名称</label>
              <input
                type="text"
                class="inputtextte"
                v-model="project.projectname"
              />
            </div>
          </div>

          <div class="inputall">
            <div class="inputleft">
              <label for="">套路名称</label>
              <input
                type="text"
                class="inputtextte"
                id="xqdzipt"
                style="width: 208px"
                v-model="project.routinename"
              />
            </div>
          </div>

          <div class="inputall" id="participant">
            <div class="inputleft">
              <label for="">参赛人员</label>
            </div>

            <div class="inputright">
              <div class="participant" v-for="item in stafflist" :key="item.id">
                <input
                  type="radio"
                  name="participant-group"
                  class="inputtextte"
                  id="xqdzipts"
                  :value="item.username"
                  @change="selectedParticipant"
                />
                <label style="text-align: left">{{ item.username }}</label>
              </div>
            </div>
          </div>

          <div class="inputall">
            <div class="inputleft">
              <label for="">（请打钩）</label>
            </div>
          </div>

          <!-- bottom -->
          <div class="inputall">
            <div class="bottomimg" @click="selectbtn" id="selectbtn">
              <img :src="'http://localhost:3002/upload/add.png'" alt="" />
              <span>项目查询</span>
            </div>
            <div class="bottomimg" @click="submitshut">
              <img :src="'http://localhost:3002/upload/add.png'" alt="" />
              <span>保存并关闭本窗</span>
            </div>
            <div class="bottomimg" style="margin-left: 15px" @click="submitgo">
              <img :src="'http://localhost:3002/upload/add.png'" alt="" />
              <span>保存并继续报名</span>
            </div>
          </div>
        </div>

        <div class="bottombox"></div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data: function () {
    return {
      project: {
        projectid: "",
        projectname: "",
        routinename: "",
        participant: "",
      },
      stafflist: [],

      updateId: 0, // 需要修改的id
      isupdateAdd: false,
    };
  },
  methods: {
    selectbtn() {},

    //保存并关闭本窗:
    submitshut() {
      // ：是否是非空字符串
      function isNonEmptyStringIfString(value) {
        return (
          typeof value !== "string" ||
          (typeof value === "string" && value.trim() !== "")
        );
      }
      if (Object.values(this.project).every(isNonEmptyStringIfString)) {
        if (this.isupdateAdd) {
          this.updategetbyid(this.updateId);
          this.$message({
            message: "修改成功",
            type: "success",
          });
        } else {
          this.addstaff();
          this.$message({
            message: "添加成功",
            type: "success",
          });
        }
        // 如果需要关闭窗口，取消注释下面的代码
        // window.close(); // 关闭窗口
      } else {
        this.$message("请填写完整信息");
      }
    },

    submitgo() {},

    selectedParticipant(e) {
      // console.log(e.target.defaultValue);
      this.project.participant = e.target.defaultValue;
    },

    // 新增 人员：
    addstaff() {
      axios({
        method: "post",
        url: "http://localhost:3002/competition/project/add",
        data: this.project,
      }).then((res) => {
        console.log(res.data);
      });
    },

    // 修改 人员
    updategetbyid(id) {
      axios({
        method: "post",
        url: "http://localhost:3002/competition/project/update",
        data: {
          id: id,
          projectid: this.project.projectid,
          projectname: this.project.projectname,
          routinename: this.project.routinename,
          participant: this.project.participant,
        },
      }).then((res) => {
        console.log(res.data);
      });
    },

    // 获取 单个人员信息
    getstaffone() {
      axios({
        url: "http://localhost:3002/competition/project/getbyId",
        method: "post",
        data: { id: this.updateId },
      }).then((res) => {
        // console.log(res.data.list[0]);
        this.project = res.data.list[0];
      });
    },

    // 获取人员信息:
    getStaff() {
      axios({
        url: "http://localhost:3002/competition/staff/list",
        method: "post",
        data: {},
      }).then((res) => {
        // console.log(res.data.list);
        this.stafflist = res.data.list;
      });
    },
  },
  mounted() {
    // 获取 修改 按钮 传递 过来的id：
    var url = window.location.href;
    var params = new URLSearchParams(url.split("?")[1]);
    var id = params.get("id");
    this.updateId = id;
    // console.log(this.updateId); // 打印出 id 参数的值
    if (id) {
      this.getstaffone();
      this.isupdateAdd = true;
    }
    this.getStaff();
  },
};
</script>

<style lang="less" scoped>
#apptb {
  //   transform: scale(0.9);
  margin: auto;
  margin-top: 40px;
  background-color: #fff;
  width: 60%;
  height: 85%;
  .grouptitle {
    width: 100%;
    height: 45px;
    background: linear-gradient(#9bcdee, #6190b5);
    color: #fff;
    display: flex;
    justify-content: center; /* 水平居中 */
    align-items: center; /* 垂直居中 */
  }
  .contentall {
    background-color: #74b0e7;
    width: 100%;
    height: 92%;
    padding-top: 6px;
    .contentallcontent {
      width: 98.5%;
      height: 99%;
      background-color: #d5e8fb;
      margin: 0 6px;
      border-radius: 10px;
      position: relative;
      .contentss {
        width: 98%;
        // height: 80%;
        margin: 0 10px;
        background-color: #eef7ff;
        transform: translateY(3%);
        border: 1px solid rgb(206, 200, 200);
        // 每一行
        .inputall {
          background-color: #eef7ff;
          margin: 0px 6px;
          height: 40px;
          font-size: 15px;
          border-bottom: 1px solid rgb(206, 200, 200);
          display: flex;
          align-items: center; /* 垂直居中 */
          select {
            height: 25px;
            width: 227px !important;
          }
          .bottomimg {
            display: flex;
            align-items: right; /* 垂直居中 */
            transform: translateX(220%);
            background: linear-gradient(#f6f6f6, #e1e1e1);
            padding: 4px 10px;
            border-radius: 5px;
            border: 1px solid rgb(174, 171, 171);
            transform: translateX(280%);
            img {
              width: 20px;
              height: 20px;
              margin-right: 8px;
            }
          }

          .inputleft {
            display: inline-block;
            vertical-align: top;
            padding: 0 6px;
            .xqdzipt {
              display: inline-block;
              width: 400px !important;
            }
            label {
              //   padding-right: 5px;
              display: inline-block;
              //   background-color: red;
              width: 80px;
              text-align: center;
            }
            .inputtextte {
              width: 220px;
            }
          }
          .inputright {
            display: inline-block;
            vertical-align: top;
            margin-left: 5px;
            label {
              //   padding-right: 5px;
              display: inline-block;
              //   background-color: red;
              width: 80px;
              text-align: center;
            }
            .inputtextte {
              width: 220px;
            }
          }
        }
      }

      .bottombox {
        background-color: #eef7ff;
        width: 100%;
        height: 18px;
        // transform: translateY(230%);
        position: absolute;
        bottom: 0;
      }
    }
  }
}

#xqdzipts {
  width: 30px !important;
}

#selectbtn {
  transform: translateX(300%) !important;
}

.participant {
  display: inline-block;
}
#participant {
  height: 100px !important;
}
</style>